package com.ronglian.iot.utils;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Component
public class TimeUtil {

    // 获取时间标准
    public static String getTime(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(date);
        return result;
    }
    // 获取时间标准
    public static String getTimeByFormat(Date date,String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String result = dateFormat.format(date);
        return result;
    }


    // 获取时间标准
    public static String getMonth(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String result = format.format(date);
        return result;
    }

    // 获取时间标准
    public static String getTimeToSecond(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = format.format(date);
        return result;
    }

    // 获取时间标准
    public static String getTimeForId(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String result = format.format(date);
        return result;
    }

    // 日期格式字符串转换成时间戳
    public static String dateToTimeStamp(String date) {
        String timeStamp="";
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            timeStamp = String.valueOf(sdf.parse(date).getTime()/1000);
        }catch (Exception e){
            e.printStackTrace();
        }

        return timeStamp;
    }

    // 日期格式字符串转换成时间
    public static Date dateStringToDate(String dateString) {
        Date date=new Date();
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             date = sdf.parse(dateString);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

    // 获取日期下周六
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 12);
        return cal.getTime();
    }

    // 获取日期所在的周一
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }


    //时间下周一生效
    public static Date getBeginDate() {
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date());
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        int day = 0;
        if (dayOfWeek == 1) {
            day = 0;
        } else {
            day = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        //修改挂单时间都为下周一生效,变更日期为新公告，2018年6月27号
        if (dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6 || dayOfWeek == 7) {
            currentDate.add(GregorianCalendar.DATE, day + 7);
            Date monday = currentDate.getTime();
            monday.setHours(0);
            monday.setMinutes(0);
            monday.setSeconds(0);
            return monday;
        } else {
            currentDate.add(GregorianCalendar.DATE, day + 14);
            Date monday = currentDate.getTime();
            monday.setHours(0);
            monday.setMinutes(0);
            monday.setSeconds(0);
            return monday;
        }

    }

    //获得past天前的日期
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR)
                - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);

        return result;
    }


    public static String getMonthLastDate() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayOfMonth = calendar.getTime();

        return TimeUtil.getTime(lastDayOfMonth);

    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }


    /**
     * 根据秒数获取时间串
     *
     * @param second (eg: 100s)
     * @return (eg : 00 : 01 : 40)
     */
    public static String getTimeStrBySecond(long second) {

        long day = second / 60 / 60 / 24;
        long hour = second / 60 / 60 % 24;
        long minute = second / 60 % 60;
        long seconds = second % 60;

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day).append("天");
        }
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        if (minute != 0) {
            sb.append(minute).append("分");
        }
        if (seconds != 0) {
            sb.append(seconds).append("秒");
        }
        return sb.toString();
    }

}
